package com.expriment.aes;

import java.security.Key;
import com.algorithm.core.AESAlgorithm;
import com.algorithm.core.FHOPEAlgorithm;
import com.algorithm.core.KeyManager;

public class DataNumExperimentAES {

    public void dataNumExperimentAES(int num, double value) {
        long lstart = 0;
        long lendTime = 0;
        // 1.----------首先测试FHOPE算法的加解密时间------------
        long sum_encTimeOfFHOPE = 0;
        long sum_decTimeOfFHOPE = 0;
        long sum_encTimeOfAES = 0;
        long sum_decTimeOfAES = 0;
        String content = "12345678";
        String password = "1234567812345678";
        int index = num;
        try {
            while (index-- > 1) {

                FHOPEAlgorithm fhopeAlg = new FHOPEAlgorithm();
                // FHOPE加密时间
                lstart = System.nanoTime();
                int encNum = 6;
                double sens = 0.1;
                WRFile wr = new WRFile();
                double[][] keys = fhopeAlg.keyGenerate(encNum);
                wr.writeKeysIntoFile(keys);
                double[][] noises = fhopeAlg.noiseGenerate(encNum, sens, keys);
                double newExtraNoise = fhopeAlg.extraNoiseGenerate();
                wr.writeENIntoFile(newExtraNoise);
                double[] encValue = fhopeAlg.encryptFunc(value, keys, noises, encNum);
                lendTime = System.nanoTime();
                sum_encTimeOfFHOPE += (lendTime - lstart);


                // FHOPE解密时间
                lstart = System.nanoTime();
                fhopeAlg.decryptFunc(encValue, keys, encNum);
                lendTime = System.nanoTime();
                sum_decTimeOfFHOPE += (lendTime - lstart);

                // 2.----------其次测试AES算法的加密时间------------------
                // AES加密时间
                lstart = System.nanoTime();
                Key detKey = KeyManager.generateDETKey(password, "id", "det");
                String encryptResult = AESAlgorithm.encrypt(content, detKey);
                lendTime = System.nanoTime();
                sum_encTimeOfAES += (lendTime - lstart);
                // AES解密时间
                lstart = System.nanoTime();
                AESAlgorithm.decrypt(encryptResult, detKey);
                lendTime = System.nanoTime();
                sum_decTimeOfAES += (lendTime - lstart);
            }
            // 3.将结果输出到Excel表格中
            ResultOfAES expOut = new ResultOfAES();
            expOut.experimentNumAES(num, sum_encTimeOfFHOPE, sum_encTimeOfAES, sum_decTimeOfFHOPE, sum_decTimeOfAES);
            System.out.println("实验二:数据量=" + num + "下FHOPE与AES算法的对比测试完成");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
